﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rocket.Core;
using Rocket.Core.Common;  
using Rocket.Windows.Framework.Data;

namespace Rocket.Windows.Framework
{
    public partial class ContainerEditor<H, V, D> : RocketContainer
        , IFormContainerEditor<V>
        where H : IHandler<V>
        where V : BaseObject
        where D : BaseDataEditor
    {
        public ContainerEditor(D dataEditorControl, IHandler<V> handler)
        {
            InitializeComponent();
            _dataEditorControl = dataEditorControl;
            _dataEditorControl.Dock = DockStyle.Fill;
            this.RocketHeight = this.Height + ((Control)_dataEditorControl).Height;
            this.groupBoxEditting.Controls.Add(_dataEditorControl);

            _handler = handler;
            this.adminController1.AddUpdateClick += new EventHandler(adminController1_AddUpdateClick);
            this.adminController1.DeleteClick += new EventHandler(adminController1_DeleteClick);
            this.adminController1.RefreshClick += new EventHandler(adminController1_RefreshClick);
            this.adminController1.CreateNewClick += new EventHandler(adminController1_CreateNewClick);

            this.SetRocketBindingSource(ref _rocketBindingSource);
            if ((OnRocketBindingSourceSetted != null)
                && (_rocketBindingSource != null))
                OnRocketBindingSourceSetted();
            RocketBindingSource.DataSourceChanged += new EventHandler(RocketBindingSourceChanged);
            ProgressBarOn = false;
            if (!string.IsNullOrEmpty(GroupBoxName))
                this.groupBoxEditting.Text = GroupBoxName;
        }

        public bool ProgressBarOn
        {
            set
            {
                this.panelProgressB.Visible =
                this.panelProgressL.Visible =
                this.panelProgressR.Visible =
                this.panelProgressTL.Visible =
                this.panelProgressT.Visible = value;
            }
        }

        public Color ProgressBarColor
        {
            set
            {
                this.panelProgressB.BackColor =
                this.panelProgressL.BackColor =
                this.panelProgressR.BackColor =
                this.panelProgressT.BackColor = value;
            }
        }

        void adminController1_CreateNewClick(object sender, EventArgs e)
        {
            if ((this.RocketAdd()) && (OnRocketTaskCompleted != null))
                OnRocketTaskCompleted(sender, e);
        }

        void adminController1_RefreshClick(object sender, EventArgs e)
        {
            if ((this.RocketRefresh()) && (OnRocketTaskCompleted != null))
                OnRocketTaskCompleted(sender, e);
        }

        void adminController1_DeleteClick(object sender, EventArgs e)
        {
            if ((this.RocketDelete()) && (OnRocketTaskCompleted != null))
                OnRocketTaskCompleted(sender, e);
        }

        void adminController1_AddUpdateClick(object sender, EventArgs e)
        {
            if ((this.RocketUpdate()) && (OnRocketTaskCompleted != null))
                OnRocketTaskCompleted(sender, e);
        }

        private D _dataEditorControl;
        public IRocketEditor<BindingSource> IRocketEditor
        {
            get { return (IRocketEditor<BindingSource>)_dataEditorControl; }
        }

        private IHandler<V> _handler;
        public IHandler<V> IHandler
        {
            get { return _handler; }
        }

        private V _bindedView;
        public virtual V BindedView
        {
            get { return (_bindedView == null) ? (V)Activator.CreateInstance(typeof(V)) : _bindedView; }
        }

        public IRocketViewer<BindingSource> IRocketViewer
        {
            get { return (IRocketViewer<BindingSource>)_dataEditorControl; }
        }

        public int ID
        {
            get { return this._dataEditorControl.ID; }
            set { this._dataEditorControl.ID = value; }
        }
        public event EventHandler OnRocketTaskCompleted;
        public event RocketBindingSourceSetted OnRocketBindingSourceSetted;
        private BindingSource _rocketBindingSource;
        public BindingSource RocketBindingSource
        {
            get { return _rocketBindingSource; }
            set
            {
                _rocketBindingSource = value;
                if ((value != null) && (OnRocketBindingSourceSetted != null))
                    OnRocketBindingSourceSetted();
            }
        }

        public bool RocketDelete()
        {
            ProgressBarOn = true;
            V mappedItem = MapIt<V>(this.ID, this._bindedView);
            if (mappedItem != null)
            {
                RocketResponse response = _handler.Remove(mappedItem);
                if (response.StatusCode != StatusCodes.Successful)
                    this.SetMessage(string.IsNullOrEmpty(response.Message)
                        ? CoreMessages.DeleteError : response.Message, MessageTypes.ErrorDisplay);
                else
                {
                    this.SetMessage(CoreMessages.DeleteDone, MessageTypes.InfomationDisplay);
                    ProgressBarOn = false;
                    return true;
                }
            }
            ProgressBarOn = false;
            return false;
        }

        public bool RocketUpdate()
        {
            ProgressBarOn = true;
            V mappedItem = MapIt<V>(this.ID, this._bindedView);
            if (mappedItem != null)
            {
                RocketResponse response = _handler.Update(mappedItem);
                if (response.StatusCode != StatusCodes.Successful)
                    this.SetMessage(string.IsNullOrEmpty(response.Message)
                        ? CoreMessages.SaveError : response.Message, MessageTypes.ErrorDisplay);
                else
                {
                    this.SetMessage(CoreMessages.SaveDone, MessageTypes.InfomationDisplay);
                    ProgressBarOn = false;
                    return true;
                }
            }
            ProgressBarOn = false;
            return false;
        }

        public bool RocketAdd()
        {
            ProgressBarOn = true;
            V mappedItem = MapIt<V>(0, this._bindedView);
            if (mappedItem != null)
            {
                RocketResponse response = _handler.Update(mappedItem);
                if (response.StatusCode != StatusCodes.Successful)
                    this.SetMessage(string.IsNullOrEmpty(response.Message)
                        ? CoreMessages.SaveError : response.Message, MessageTypes.ErrorDisplay);
                else
                {
                    this.SetMessage(CoreMessages.SaveDone, MessageTypes.InfomationDisplay);
                    ProgressBarOn = false;
                    return true;
                }
            }
            ProgressBarOn = false;
            return false;
        }

        public bool RocketLoad()
        {
            this.RocketBindingSource.ResetBindings(false);
            return true;
        }

        public bool RocketRefresh()
        {
            this._dataEditorControl.Refresh();
            return true;
        }

        public void SetRocketBindingSourceView(V dataSource)
        {
            _bindedView = dataSource;
            RocketBindingSource.DataSource = dataSource;
        }

        public void SetRocketBindingSource(ref BindingSource bindingSource)
        {
            this._dataEditorControl.SetRocketBindingSource(ref bindingSource);
        }

        public virtual T MapIt<T>(int Id, T bindedView) where T : BaseObject { return this._dataEditorControl.MapIt<T>(Id, bindedView); }
        public virtual void RocketBindingSourceChanged(object sender, EventArgs e)
        {
            this._dataEditorControl.RocketBindingSourceChanged((object)this.BindedView, e);
        }

        public string GridViewGroupBoxName { get { return _dataEditorControl.GridViewGroupBoxName; } set { _dataEditorControl.GridViewGroupBoxName = value; } }
        public string ManagerGroupBoxName { get { return _dataEditorControl.ManagerGroupBoxName; } set { _dataEditorControl.ManagerGroupBoxName = value; } }
        public string GroupBoxName { get { return _dataEditorControl.GroupBoxName; } set { _dataEditorControl.GroupBoxName = value; } }

        public void ValidateInput<X>(X bindedView) where X : class, new()
        {
            //throw new NotImplementedException();
        }
    }
}
